/** @file   pacer.c
    @author M. P. Hayes, UCECE
    @date   21 August 2007
    @brief  Support for paced loops on ATmega8.  
*/
#include "config.h"
#include "pacer.h"

static uint16_t pacer_ticks;

/* Initialise pacer for desired tick rate in Hz.  */
void pacer_init (pacer_rate_t pacer_rate)
{
    TCCR1A = 0x00;
    /* Set up timer1 with prescaler of 1.  */
    TCCR1B = 0x01;
    TCNT1 = 0;

    pacer_ticks = (uint32_t) F_CPU / pacer_rate;
}

/* Wait until next pacer tick.  */
void pacer_wait (void)
{
    while (TCNT1 < pacer_ticks)
	continue;

    TCNT1 = 0;
}
